package com.patsnap.data.npd.dw.handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TimestampLongTypeHandler extends BaseTypeHandler<Long> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Long timestamp, JdbcType jdbcType) throws SQLException {
        preparedStatement.setTimestamp(i, new java.sql.Timestamp(timestamp));
    }

    @Override
    public Long getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
        java.sql.Timestamp timestamp = resultSet.getTimestamp(columnName);
        return timestamp != null ? timestamp.getTime() : null;
    }

    @Override
    public Long getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
        java.sql.Timestamp timestamp = resultSet.getTimestamp(columnIndex);
        return timestamp != null ? timestamp.getTime() : null;
    }

    @Override
    public Long getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
        java.sql.Timestamp timestamp = callableStatement.getTimestamp(columnIndex);
        return timestamp != null ? timestamp.getTime() : null;
    }
}
